package com.zju.DoctorGo.controller;

import com.zju.DoctorGo.entity.Medicine;
import com.zju.DoctorGo.entity.Result;
import com.zju.DoctorGo.service.MedicineService;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@CrossOrigin
@RestController
@Slf4j
@RequestMapping("/api/medicines")
public class MedicineController {

    private static final Logger log = LoggerFactory.getLogger(MedicineController.class);
    @Autowired
    private MedicineService medicineService;

    @PostMapping
    public Result createMedicine(@RequestBody Medicine medicine) {
        log.info("Creating medicine: {}", medicine);
        try {
            medicineService.createMedicine(medicine);
            return Result.success();
        } catch (DuplicateKeyException e) {
            log.error("Failed to create medicine due to duplicate key: {}", e.getMessage());
            return Result.error("A medicine with the same ID already exists. Please use a unique ID.");
        } catch (Exception e) {

            log.error("Failed to create medicine: {}", e.getMessage());
            return Result.error("An unexpected error occurred while creating the medicine.");
        }
    }

    @GetMapping("/{id}")
    public Result getMedicine(@PathVariable Long id) {
        log.info("Fetching medicine with ID: {}", id);
        try {
            Medicine medicine = medicineService.getMedicine(id);
            if (medicine != null) {
                return Result.success(medicine);
            } else {
                return Result.error("Medicine not found.");
            }
        } catch (Exception e) {
            log.error("Failed to fetch medicine: {}", e.getMessage());
            return Result.error("An error occurred while fetching medicine.");
        }
    }

    @PutMapping("/{id}")
    public Result updateMedicine(@PathVariable Long id, @RequestBody Medicine medicine) {
        log.info("Updating medicine with ID: {}", id);
        try {
            medicine.setMedicineId(id);
            medicineService.updateMedicine(id, medicine);
            return Result.success();
        } catch (Exception e) {
            log.error("Failed to update medicine: {}", e.getMessage());
            return Result.error("An error occurred while updating medicine.");
        }
    }

    @DeleteMapping("/{id}")
    public Result deleteMedicine(@PathVariable Long id) {
        log.info("Deleting medicine with ID: {}", id);
        try {
            medicineService.deleteMedicine(id);
            return Result.success();
        } catch (Exception e) {
            log.error("Failed to delete medicine: {}", e.getMessage());
            return Result.error("An error occurred while deleting medicine.");
        }
    }

    @GetMapping
    public Result getAllMedicines() {
        log.info("Fetching all medicines");
        try {
            List<Medicine> medicines = medicineService.getAllMedicines();
            return Result.success(medicines);
        } catch (Exception e) {
            log.error("Failed to fetch medicines: {}", e.getMessage());
            return Result.error("An error occurred while fetching medicines.");
        }
    }



}

